import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import entities.Articulo;
import entities.ArticuloVO;
import facade.Facade;
import facade.FacadeBean;

@Path("/bestSeller")
public class EjemploRest {

//	@EJB
//	Facade adminBestSeller;
	
	//Ejemplo recibiendo un parametro por GET
	@POST
	@Path("/procesar")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.TEXT_PLAIN)
	public String informarMasVendidos(String json) {
	Facade adminBestSeller = invocarFacade();
	adminBestSeller.actualizarRanking(json);
		
		
		return "Completado";
	}

	private Facade invocarFacade(){
	Context context = null;
    Facade bean = null;
    try {
        // 1. Obtaining Context
        context = ClientUtility.getInitialContext();
        // 2. Generate JNDI Lookup name
        String lookupName = getLookupName();
        // 3. Lookup and cast
        bean = (Facade) context.lookup(lookupName);

    } catch (NamingException e) {
        e.printStackTrace();
    }
    return bean;
}

private static String getLookupName() {
/*
The app name is the EAR name of the deployed EJB without .ear suffix.
Since we haven't deployed the application as a .ear,
the app name for us will be an empty string
*/
    String appName = "PortalWeb_EAR";

    /* The module name is the JAR name of the deployed EJB
    without the .jar suffix.
    */
    String moduleName = "PortalWeb_Server";

/*AS7 allows each deployment to have an (optional) distinct name.
This can be an empty string if distinct name is not specified.
*/
    String distinctName = "";

    // The EJB bean implementation class name
    String beanName = FacadeBean.class.getSimpleName();

    // Fully qualified remote interface name
    final String interfaceName = Facade.class.getName();

    // Create a look up string name
    String name = "ejb:" + appName + "/" + moduleName + "/" +
        distinctName    + "/" + beanName + "!" + interfaceName;

    return name;
}
}
class ClientUtility {
 
    private static Context initialContext;
 
    private static final String PKG_INTERFACES = "org.jboss.ejb.client.naming";
 
    public static Context getInitialContext() throws NamingException {
        if (initialContext == null) {
            Properties properties = new Properties();
            properties.put(Context.URL_PKG_PREFIXES, PKG_INTERFACES);
 
            initialContext = new InitialContext(properties);
        }
        return initialContext;
    }
}
